// ui.js
"ui";

var color = "#4EBFDD";


ui.layout(
    <drawer id="drawer" >
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="脚本工具箱" />
                <tabs id="tabs" />
            </appbar>
            <viewpager id="viewpager" h="auto">
                <frame>
                    <ScrollView>
                        <vertical marginTop="5">
                            <card w="*" h="auto" margin="10 5" cardCornerRadius="2dp" cardElevation="1dp" gravity="center_vertical">
                                <vertical padding="18 8" h="auto">
                                    <linear>
                                        <Switch margin="12 0" layout_weight="1" id="autoService" text="无障碍服务" textSize="15sp" checked="{{auto.service != null}}" />
                                        <Switch margin="12 0" layout_weight="1" id="floatyService" text="悬浮窗权限" textSize="15sp" checked="false" />
                                    </linear>
                                </vertical>
                                <View bg="#4EBFDD" h="*" w="5" />
                            </card>
                            <card w="*" h="auto" margin="10 5" cardCornerRadius="2dp" cardElevation="1dp" gravity="center_vertical">
                                <vertical padding="16" h="auto">
                                    <text textSize="16sp" textStyle="bold" textColor="#FFA500" >【直播间挂机】</text>
                                    <horizontal>
                                        <text textSize="16sp">挂机的软件</text>
                                        <spinner id="appSelect" entries="抖音直播挂机|快手直播挂机" />
                                    </horizontal>
                                    <text text="挂机的时间/分钟" textColor="black" textSize="16sp" marginTop="16" />
                                    <input id="hangupTime" inputType="number" textColor="#4682B4" />
                                    <text text="点赞的次数" textColor="black" textSize="16sp" marginTop="16" />
                                    <input id="likeCount" inputType="number" textColor="#4682B4" />
                                    <text text="需要截屏的次数" textColor="black" textSize="16sp" marginTop="16" />
                                    <input id="screenTakenCount" inputType="number" textColor="#4682B4" />
                                    <text text="截屏间隔/分钟" textColor="black" textSize="16sp" marginTop="16" />
                                    <input id="screenTakenTime" inputType="number" textColor="#4682B4" />
                                    <button id="hangUpStart" style="Widget.AppCompat.Button.Colored" text="开 始 执 行" />
                                </vertical>
                                <View bg="#4EBFDD" h="500" w="5" />
                            </card>
                        </vertical>
                    </ScrollView>
                </frame>
                <frame>
                    <ScrollView>
                        <vertical marginTop="5">
                            <card w="*" h="auto" margin="10 5" cardCornerRadius="2dp" cardElevation="1dp" gravity="center_vertical">
                                <vertical padding="16" h="auto">
                                    <text textSize="16sp" textStyle="bold" textColor="#FFA500" >【快手极速版刷金币】</text>
                                    <button id="getKsGold" style="Widget.AppCompat.Button.Colored" text="开 始 执 行" />
                                </vertical>
                                <View bg="#4EBFDD" h="*" w="5" />
                            </card>
                        </vertical>
                    </ScrollView>
                </frame>
                <frame>
                    <text text="第三页内容" textColor="green" textSize="16sp" />
                </frame>
            </viewpager>
        </vertical>
        <vertical layout_gravity="left" bg="#ffffff" w="280">
            <img w="280" h="200" scaleType="fitXY" src="http://images.shejidaren.com/wp-content/uploads/2014/10/023746fki.jpg" />
            <list id="menu">
                <horizontal bg="?selectableItemBackground" w="*">
                    <img w="50" h="50" padding="16" src="{{this.icon}}" tint="{{color}}" />
                    <text textColor="black" textSize="15sp" text="{{this.title}}" layout_gravity="center" />
                </horizontal>
            </list>
        </vertical>
    </drawer>
);


activity.setSupportActionBar(ui.toolbar);

// 设置滑动页面的标题
ui.viewpager.setTitles(["基础工具", "高级工具", "其他工具"]);
// 让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);

// 让工具栏左上角可以打开侧拉菜单
ui.toolbar.setupWithDrawer(ui.drawer);

ui.menu.setDataSource([
    {
        title: "关于",
        icon: "@drawable/ic_android_black_48dp"
    },
    {
        title: "选项二",
        icon: "@drawable/ic_settings_black_48dp"
    },
    {
        title: "选项三",
        icon: "@drawable/ic_favorite_black_48dp"
    },
    {
        title: "退出",
        icon: "@drawable/ic_exit_to_app_black_48dp"
    }
]);

ui.menu.on("item_click", item => {
    switch (item.title) {
        case "退出":
            ui.finish();
            break;
    }
});

ui.autoService.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({ action: "android.settings.ACCESSIBILITY_SETTINGS" });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});

// 定义一个函数来设置按钮点击事件
// ui.ks_hangup.click(() => {
//     methods.ksHangUp();
// });
ui.hangUpStart.on("click", () => {
    try {
        var appSelect = ui.appSelect.getSelectedItemPosition();
        var hangupTime = parseInt(ui.hangupTime.text(), 10);
        var likeCount = parseInt(ui.likeCount.text(), 10);
        var screenTakenCount = parseInt(ui.screenTakenCount.text(), 10);
        var screenTakenTime = parseInt(ui.screenTakenTime.text(), 10);

        if (appSelect != null && !isNaN(hangupTime) && !isNaN(likeCount) && !isNaN(screenTakenCount) && !isNaN(screenTakenTime)) {
            var methods_url = "https://gitee.com/worker-xiao-zhang/script-toolbox/raw/master/methods.js";
            var tools_url = "https://gitee.com/worker-xiao-zhang/script-toolbox/raw/master/tools.js";

            // 异步方式进行网络请求和文件写入
            threads.start(function () {
                // 下载并写入 methods.js 文件
                var res1 = http.get(methods_url);
                if (res1.statusCode == 200) {
                    var methodsContent = res1.body.string();
                    var methodsFilePath = "./methods.js";  // 保存路径根据实际情况修改
                    files.write(methodsFilePath, methodsContent);
                } else {
                    toast("请求 methods.js 失败");
                    return;
                }

                // 下载并写入 tools.js 文件
                var res2 = http.get(tools_url);
                if (res2.statusCode == 200) {
                    var toolsContent = res2.body.string();
                    var toolsFilePath = "./tools.js";  // 保存路径根据实际情况修改
                    files.write(toolsFilePath, toolsContent);
                } else {
                    toast("请求 tools.js 失败");
                    return;
                }

                // 加载并执行方法
                var methods = require(methodsFilePath);
                methods.hangUp(appSelect, hangupTime, likeCount, screenTakenCount, screenTakenTime);

                // 在子线程中进行 UI 操作需要使用 ui.run 方法
                ui.run(function () {
                    toast("脚本更新完成，正在加载。。");
                });
            });
        } else {
            toast("请输入所有参数");
        }
    } catch (error) {
        console.error("Error:", error);
        toast("发生错误，请检查输入");
    }
});

//ks 
ui.getKsGold.on("click", () => {
    var ksapp_url = "https://gitee.com/worker-xiao-zhang/script-toolbox/raw/master/ksapp.js";
    var tasks_url = "https://gitee.com/worker-xiao-zhang/script-toolbox/raw/master/Tasks.js";
    // 异步方式进行网络请求和文件写入
    threads.start(function () {
        // 下载并写入 ksapp.js 文件
        var res1 = http.get(ksapp_url);
        if (res1.statusCode == 200) {
            var ksappContent = res1.body.string();
            var ksappFilePath = "./ksapp.js";  // 保存路径根据实际情况修改
            files.write(ksappFilePath, ksappContent);
        } else {
            toast("请求 ksapp.js 失败");
            return;
        }

        // 下载并写入 tasks.js 文件
        var res2 = http.get(tasks_url);
        if (res2.statusCode == 200) {
            var tasksContent = res2.body.string();
            var tasksFilePath = "./Tasks.js";  // 保存路径根据实际情况修改
            files.write(tasksFilePath, tasksContent);
        } else {
            toast("请求 Tasks.js 失败");
            return;
        }
        //如果存在
        if (files.exists(ksappFilePath) && files.exists(tasksFilePath)) {
            engines.execScriptFile(ksappFilePath);
        } else {
            log("文件不存在");
        }


    })
})



// 添加按键监听事件，用于停止程序
// 设置监听音量减键
events.observeKey();
events.onKeyDown("volume_down", function (event) {
    toast("音量减按下，脚本终止");
    engines.stopAllAndToast(); // 停止所有脚本并显示Toast提示
});
